package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.constant.GlobalConstant;
import com.heima.common.exception.CustomException;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.model.user.pojos.ApUserFan;
import com.heima.model.user.pojos.ApUserFollow;
import com.heima.user.mapper.ApUserFanDao;
import com.heima.user.service.ApUserFanService;

import com.heima.user.service.ApUserFollowService;
import com.heima.user.service.ApUserService;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * APP用户粉丝信息表 服务实现类
 * </p>
 *
 * @author kzj
 * @since 2024-01-15
 */
@Service
@Transactional
public class ApUserFanServiceImpl extends ServiceImpl<ApUserFanDao, ApUserFan> implements ApUserFanService {
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    ApUserService apUserService;
    @Autowired
    ApUserFollowService apUserFollowService;

    @Override
    public boolean addFan(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        boolean b = this.addFanToRedis(fanId, userId);
        if (b)
            this.addFanToDB(fanId,userId);
        return b;
    }

    @Override
    public boolean removeFan(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        boolean b = this.removeFanToRedis(fanId, userId);
        if (b)
            this.removeFanToDB(fanId,userId);
        return b;
    }


    public boolean addFanToRedis(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        String followKey=GlobalConstant.FOLLOW_PREFIX+userId;
//        String fanKey= GlobalConstant.FAN_PREFIX+fanId;
        return stringRedisTemplate.opsForSet().add(followKey,fanId.toString())!=0;
//        stringRedisTemplate.opsForSet().add(fanKey,userId.toString());
    }

    public void addFanToDB(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        ApUser fan = apUserService.getById(fanId);
        ApUser follow = apUserService.getById(userId);
        if (fan==null||follow==null)
        {
            throw new CustomException(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        ApUserFan apUserFan = new ApUserFan();
        apUserFan.setUserId(follow.getId());
        apUserFan.setFansId(fan.getId());
        apUserFan.setFansName(fan.getName());
        apUserFan.setLevel(ApUserFan.Level.NORMAL.getValue());
        apUserFan.setCreatedTime(new Date());
        apUserFan.setIsDisplay(GlobalConstant.TRUE);
        apUserFan.setIsShieldLetter(GlobalConstant.FALSE);
        apUserFan.setIsShieldComment(GlobalConstant.FALSE);
        ApUserFollow apUserFollow = new ApUserFollow();
        apUserFollow.setUserId(fan.getId());
        apUserFollow.setFollowId(follow.getId());
        apUserFollow.setFollowName(follow.getName());
        apUserFollow.setCreatedTime(new Date());
        apUserFollow.setLevel(ApUserFollow.Level.OCCASIONAL.getValue());
        apUserFollow.setIsNotice(GlobalConstant.TRUE);
        this.save(apUserFan);
        apUserFollowService.save(apUserFollow);
    }


    public boolean removeFanToRedis(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        String followKey=GlobalConstant.FOLLOW_PREFIX+userId;
//        String fanKey= GlobalConstant.FAN_PREFIX+fanId;
        return stringRedisTemplate.opsForSet().remove(followKey, fanId.toString())!=0;
//        stringRedisTemplate.opsForSet().remove(fanKey,userId.toString());
    }


    public void removeFanToDB(Integer fanId, Integer userId) {
        if (fanId==null||userId==null)
        {
            throw new CustomException(AppHttpCodeEnum.PARAMS_NOT_NULL);
        }
        ApUser fan = apUserService.getById(fanId);
        ApUser follow = apUserService.getById(userId);
        if (fan==null||follow==null)
        {
            throw new CustomException(AppHttpCodeEnum.USER_NOT_EXIST);
        }
        this.remove(Wrappers.<ApUserFan>lambdaQuery().eq(ApUserFan::getFansId,fanId).eq(ApUserFan::getUserId,userId));
        apUserFollowService.remove(Wrappers.<ApUserFollow>lambdaQuery().eq(ApUserFollow::getUserId,fanId).eq(ApUserFollow::getFollowId,userId));
    }
}
